class Movie < ActiveRecord::Base
	validates_presence_of :title  
	validates_presence_of :description
	validates_uniqueness_of :title
	validates_length_of :description, :minimum => 10 , :too_short => "Movie description must be at least 10 characters long"
	validates_inclusion_of :rating, :in => %w(G PG PG-13 R NC-17)
	
	def appropriate_for_birthdate?(birthdate)
		if %w[G PG].include?(rating)
			return true
		end
		current_time = Time.now
		age = (current_time.year - 1 - birthdate.year)
		age += 1 if ((current_time.month-birthdate.month)>=1 &&(current_time.day-birthdate.day)>=1)
	
		#age = Time.at(Time.now - birthdate).year - 1970

		if (age >= 17) 
			return true
		elsif (rating == "PG-13") && age >= 13
			return true
		else
			return false
		end				
	end

	def Movie.find_all_appropriate_for_birthdate(birthdate)
		allowed_movies=["G","PG"]
		#age = Time.at(Time.now - birthdate).year - 1970
		age = (Time.now.year - 1 - birthdate.year)
		age += 1 if ((Time.now.month-birthdate.month)>=1 &&(Time.now.day-birthdate.day)>=1)
		
		if (age >= 17)
			allowed_movies.push("PG-13","R","NC-17")
		elsif (age >= 13)
			allowed_movies.push("PG-13")
		end
		
		find(:all, :conditions => { :rating => allowed_movies})
	end
	
end
